<?php
defined("DS")||define("DS",DIRECTORY_SEPARATOR);
defined("G_FRAMEWORKSDIR")||define("G_FRAMEWORKSDIR",realpath(dirname(__FILE__)).DS);
include G_FRAMEWORKSDIR."core.php";
class GHF{
    static $_self;
    public $_controller;
    public $_action;
    public $_config;//应用的配置
    private function __construct($apppath){
        //项目应用下的文件目录地址
        define("APP_ROOT",realpath(dirname($apppath)).DS);
        $this->_config = G_Config::load(APP_ROOT."config".DS."config.ini",'ini');//加载配置文件
        $this->__init__();//程序初始化
    }
    
    public static function init($apppath = null){
        if(isset(self::$_self) === false){
            self::$_self = new GHF($apppath);
        }
        return self::$_self;
    }
    public function run(){
        $__r__ = isset($_GET['__r__']) ? trim($_GET['__r__']) : "/index/index";
        $__r__ = str_replace(".htm","",$__r__);
        $_rs = explode("/",$__r__);
        $this->_controller = isset($_rs[1]) && !empty($_rs[1]) ? ucfirst($_rs[1]) : "Index";
        $action = $this->_action = isset($_rs[2]) && !empty($_rs[2]) ? strtolower($_rs[2]) : "index";
        $len = count($_rs);
        if($len > 3){
            //处理URL中GET数据
            for($i = 3 ; $i < $len ; $i++){
                $pos = strpos($_rs[$i],"-");
                if($pos>= 0){
                    $_GET[substr($_rs[$i],0,$pos)] = substr($_rs[$i],$pos+1);
                }
            }
        }//var_dump($this->_controller,$this->_action);
        $cpath = APP_ROOT.'controller'.DS.$this->_controller.'.controller.php';
        if(file_exists($cpath)){
            include $cpath;
            $cname = $this->_controller."Controller";
            $controller = new $cname($this);
            $action .= "Action";
            $controller->$action();
        }else{
            echo $cpath;
            var_dump($_REQUEST);
            die();
        }
    }
    protected function __init__(){
        //设置时区
        date_default_timezone_set($this->_config->getKey("default_time_zone",'Asia/Shanghai'));
        //是否自动开启session
        if($this->_config->getKey("auto_session_start",false)){
            session_start();
        }
    }
}


